﻿#region

using System;
using System.Linq;
using System.Text;
using Domain;

#endregion

namespace Presentation {
    public class PEGraph<T> where T : IEdge, new() {
        private string stringRepresentaion;

        public PEGraph(Graph<T> graph) {
            DomainObject = graph;
            ConvertGraphToString(graph);
        }

        public PEGraph(string s) {
            Text = s;
        }

        private void ConvertGraphToString(Graph<T> graph) {
            var builder = new StringBuilder();
            builder
                .Append(graph.NumberVertices)
                .AppendLine()
                .Append(graph.NumberEdges)
                .AppendLine();

            foreach (var edge in graph.Edges) {
                foreach (var vertex in edge.Vertices) {
                    builder.AppendFormat("{0}   ", vertex);
                }

                builder.AppendLine();
            }

            stringRepresentaion = builder.ToString();
        }

        private Graph<T> domainObject;

        public Graph<T> DomainObject {
            get { return domainObject; }
            set {
                ConvertGraphToString(value);
                domainObject = value;
            }
        }

        public string Text {
            get { return stringRepresentaion; }
            set {
                var strings = value.Split(new[] {"\r\n"}, StringSplitOptions.RemoveEmptyEntries);

                if (strings.Length < 2) throw new ApplicationException("Неправильный формат задания графа");
                if (strings.Length - 2 != int.Parse(strings[1]))
                    throw new ApplicationException("Неправильный формат задания графа");

                var numberVertices = int.Parse(strings[0]);

                var vertices1 = new Vertex[numberVertices];
                for (var i = 0; i < numberVertices; i++) {
                    vertices1[i] = new Vertex(i);
                }

                DomainObject = new Graph<T>(vertices1.ToList());
                for (var i = 2; i < strings.Length; i++) {
                    var s = strings[i];
                    var vertices = s.Split(' ')
                        .Where(x => x.Length > 0)
                        .Select(x => int.Parse(x))
                        .Select(x => vertices1[x])
                        .ToList();

                    if (vertices.Count < 2) throw new ApplicationException("Ребро не может иметь меньше двух вершин");

                    DomainObject.AddEdge(new T {Vertices = vertices});
                }

                stringRepresentaion = value;
            }
        }
    }

    public class PEHyperGraph : PEGraph<HyperEdge> {
        public PEHyperGraph(Graph<HyperEdge> graph) : base(graph) {}
        public PEHyperGraph(string s) : base(s) {}
    } ;

    public class PEGraph : PEGraph<Edge> {
        public PEGraph(Graph<Edge> graph) : base(graph) {}
        public PEGraph(string s) : base(s) {}
    } ;
}